from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Activation
from tensorflow.keras import utils
from tensorflow.keras import losses, optimizers, metrics
import tensorflow as tf
import numpy as np
from sklearn.model_selection import train_test_split

# Predicting animal type based on various features
xy = np.loadtxt('data-04-zoo.csv', delimiter=',', dtype=np.float32)
x_data = xy[:, 0:-1]
y_data = xy[:, [-1]]
# print(y_data)
x_train, x_test, y_train, y_test = train_test_split(x_data, y_data)
# print(x_data.shape, y_data.shape)

nb_classes = 7
# y_one_hot = utils.to_categorical(y_data, nb_classes)

model = Sequential()
model.add(Dense(nb_classes, input_shape=(16,)))
model.add(Activation('softmax'))

model.summary()

#配置模型:SparseCategoricalCrossentropy不需要独热, 优化器RMSprop
model.compile(loss=losses.SparseCategoricalCrossentropy,
              optimizer=optimizers.RMSprop(lr=0.1),
              metrics=['acc'])

#训练模型,加入验证集
model.fit(x_train, y_train, validation_data=(x_test, y_test), epochs=100)


